package com.example.sixthweek;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.provider.Telephony;
import android.util.Log;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;

public class MyTask implements Runnable{
    private static final String TAG = "myclick";
    private Handler handler;
    public void setHandler(Handler handler){
        this.handler = handler;
    }
    @Override
    public void run() {
        //获取网络数据并返回

            Log.i(TAG, "run: Running....");

            //do...
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            RateListActivity logDate = new RateListActivity();
            RateListActivity rateListActivity = new RateListActivity();
            List<String> retList = new ArrayList<String>();
            Message msg = handler.obtainMessage();
            String curDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
            Log.i(TAG, "curDateStr: "+curDateStr + "logDate:"+ logDate);
            if(curDateStr.equals(logDate)){
                //如果相等，则不从网络中获取数据
                Log.i(TAG, "run: 日期相等，从数据库中获取数据");

                DBManager dbManager = new DBManager(rateListActivity);
                for(RateItem rateItem : dbManager.listAll()){
                    retList.add(rateItem.getCurName()+"=>" + rateItem.getCurRate());
                }

            }else{
                Log.i(TAG, "日期不等，从网络中获取在线数据");
                //获取网络数据
                try {
                    List<RateItem> rateList = new ArrayList<RateItem>();
                  Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
//                String title = doc.title();
//                Log.i(TAG, "run: title=" + title);
//                Elements trs = doc.getElementsByTag("tr");
//                for (Element tr : trs) {
//                    Elements tds = tr.getElementsByTag("td");
//                    Log.i(TAG, "run: td.size=" + tds.size());
//                    if (tds.size() > 0) {
//                        String td1 = tds.get(0).text();
//                        String td2 = tds.get(5).text();
//                        rateList.add(td1+"==>"+td2);
//                    }
//                }
//                Elements tds = doc.getElementsByTag("td");
//                for(int i=8;i < tds.size();i+=8){
//                      String td1 = tds.get(i).text();
//                      String td2 = tds.get(i+5).text();
//                      Log.i(TAG, "run: "+ td1+"==>"+td2);
//                      rateList.add(td1 + "==>" + td2);
//                   }
                Log.i(TAG, "run: "+retList);
                    Elements tables = doc.getElementsByTag("table");
                    Element table2 = tables.get(1);
                    Elements trs = table2.getElementsByTag("tr");
                    for (Element tr : trs){
                        Elements tds = tr.getElementsByTag("td");
                        if(tds.size()>0){
                            String td1 = tds.get(0).text();
                            String td2 = tds.get(5).text();
                            Log.i(TAG, "run: "+ td1 + "==>" + td2);
                            retList.add(td1 + "==>" + td2);
                            RateItem rateItem = new RateItem(td1,td2);
                            rateList.add(rateItem);
                        }
                    }
                    DBManager dbManager = new DBManager(rateListActivity);
                    dbManager.deleteAll();
                    Log.i("db", "删除所有记录");
                    dbManager.addALL(rateList);
                    Log.i("db", "添加新记录集");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //更新记录日期
                SharedPreferences sp = logDate.getSharedPreferences("myrate", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                RateListActivity DATE_SP_KEY = new RateListActivity();
                edit.putString(String.valueOf(DATE_SP_KEY), curDateStr);
                edit.commit();
                Log.i("run", "更新日期结束："+ curDateStr);
            }
             msg.obj = retList;
             handler.sendMessage(msg);





        }
    }

